<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, maximum-scale=1.0" />
    <title>登录</title>
    <link rel="stylesheet" href="css/register.css" />
    <style>

    </style>
</head>

<body>
    <div id="register-bg">
        <div id="register">
            <div id="register-title">注册</div>
            <div class="register-form-input">
                <img class="register-form-input-icon" src="images/username.png" />
                <input type="text" id="username" placeholder="邮箱">
            </div>
            <div class="register-form-input">
                <img class="register-form-input-icon" src="images/password.png" />
                <input type="password" id="password" placeholder="密码">
            </div>
            <div class="register-form-input">
                <img class="register-form-input-icon" src="images/password.png" />
                <input type="password" id="confirm-password" placeholder="确认密码">
            </div>
            <div class="register-form-input">
                <img class="register-form-input-icon" src="images/password.png" />
                <input type="text" id="code" placeholder="验证码">
                <button class="get-code" id="codeBtn" onclick="getCode()">获取验证码</button>
            </div>
            <button class="register-form-btn" onclick="register()">注册</button>
        </div>
    </div>
</body>

<!-- Various Scripts -->
<script src="./js/jquery-3.2.1.min.js"></script>

<script>
    function getCode() {
        var email = $('#username').val();
        if (email == "") {
            alert("请输入邮箱再发送验证码!");
            return;
        }
        var that = $('#codeBtn');
        console.log(that)
        var timeo = 60;
        var timeStop = setInterval(function () {
            timeo--;
            if (timeo > 0) {
                that.text('重新发送' + timeo + 's');
                that.attr('disabled', 'disabled');
            } else {
                timeo = 60;//当减到0时赋值为60
                that.text('获取验证码');
                clearInterval(timeStop);//清除定时器
                that.removeAttr('disabled');//移除属性，可点击
            }
        }, 1000)
        var url = "http://localhost/api/mail/sendVerificationCode/" + email;
        $.ajax({
            //请求方式
            type: "POST",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            url: url,
            xhrFields: {
                withCredentials: true
            },
            data: "",
            success: function () {
                alert("发送验证码成功!");
            },
            //请求失败，包含具体的错误信息
            error: function (e) {
                alert("获取验证码失败！");
            }
        });
    }
    function isJSON(str) {
    if (typeof str == 'string') {
        try {
            var obj=JSON.parse(str);
            if(typeof obj == 'object' && obj ){
                return true;
            }else{
                return false;
            }

        } catch(e) {
            console.log('error：'+str+'!!!'+e);
            return false;
        }
    }
    console.log('It is not a string!')
}

    function register() {
        var email = $('#username').val();
        var password = $('#password').val();
        var confirmWord = $('#confirm-password').val();
        var code = $('#code').val();
        if (password != confirmWord) {
            alert("两次输入的密码不匹配，请重试！");
            return;
        }
        if (email == "") {
            alert("请输入邮箱再进行注册!");
            return;
        }
        var param = {};
        param.eamil = email;
        param.password = password;
        param.code = code;
        var url = "http://localhost/api/user/register/" + email + "/" + password + "/" + code;
        $.ajax({
            //请求方式
            type: "POST",
            //请求的媒体类型
            contentType: "application/json;charset=UTF-8",
            url: url,
            xhrFields: {
                withCredentials: true
            },
            data: "",
            success: function (data) {
                //data = JSON.parse(data);
                if(isJSON(data)){
                    data = JSON.parse(data);
                }
                if (data.code == '0000') {
                    alert("注册成功！");
                    window.location.href = "welcome.html";
                } else {
                    alert("注册失败，原因为：" + data.msg);
                }
            },
            //请求失败，包含具体的错误信息
            error: function (e) {
                alert("注册失败");
            }
        });
        //window.location.href = "login.html";
    }
</script>

</html>